define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    "index_url": "basic/students/index",
                    "add_url": "basic/students/add",
                    "edit_url": "basic/students/edit",
                    "del_url": "basic/students/del",
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'username', title: '学生名称'},
                        {field: 'avatar', title: '学生头像', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'mobile', title: '学生电话'},
                        {field: 'email', title: '学生邮箱'},
                        {  
                            field: 'student_classes', 
                            title: '所属班级',
                            formatter: function(value, row, index) {
                                return value && value.length > 0 ? value[0].name : '无班级';
                            }
                        },
                        {field: 'school.name', title: '所属学校'},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: '状态', formatter: Table.api.formatter.status, searchList: { normal: __('正常'), hidden: __('异常') }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
